﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace BBPA.BLL
{
    public partial class Config
    {
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="model_">已经初始化的对象</param>
        public object GetModel(EnumHelper.Config _enumConfig, object model_)
        {
            List<Model.Config> listModelConfig = GetModelList ("  ModelName='" + _enumConfig.ToString()+"'");
            if (listModelConfig.Count > 0)
            {
                string XMLstr = DataTypePlus.requestString(listModelConfig[0].Data);
                if (XMLstr == "")
                {
                    using (StringWriter sw = new StringWriter())
                    {
                        XmlSerializer xz = new XmlSerializer(model_.GetType());
                        xz.Serialize(sw, model_);
                        XMLstr = sw.ToString();
                    }
                }
                //反序
                XmlDocument modelXML = new XmlDocument();
                modelXML.LoadXml(XMLstr);
                foreach (XmlNode node in modelXML.SelectSingleNode(model_.GetType().Name).ChildNodes)
                {
                    System.Reflection.PropertyInfo[] properties = model_.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    int i = 0;
                    foreach (System.Reflection.PropertyInfo item in properties)//遍历model_
                    {
                        string name = item.Name;
                        if (node.Name == name)
                        {
                            if (node.InnerText != "[Null]")
                            {
                                if (item.PropertyType == typeof(System.Guid))
                                    item.SetValue(model_, new Guid(node.InnerText), null);
                                else
                                    item.SetValue(model_, Convert.ChangeType(node.InnerText, item.PropertyType), null);
                            }
                            else
                                item.SetValue(model_, null, null);
                        }
                        i++;
                    }
                }

            }
            return model_;
        }
        /// <summary>
        /// 更新
        /// </summary>
        public void Update(EnumHelper.Config _enumConfig, object model_)
        {
            Model.Config modelConfig = new Model.Config();
            List<Model.Config> listModelConfigs = GetModelList ("  ModelName='" + _enumConfig.ToString()+"'");
            if (listModelConfigs.Count > 0) modelConfig = listModelConfigs[0];
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xz = new XmlSerializer(model_.GetType());
                xz.Serialize(sw, model_);
                modelConfig.Data = sw.ToString();
                
            }
            if (listModelConfigs.Count == 0)
            {
                modelConfig.ModelName = _enumConfig.ToString();
                Add(modelConfig);
            }
            else Update(modelConfig); ;

            
            
        }
    }
}
